package com.lee.dutylower.db.greendao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;

import com.lee.dutylower.db.greenEntity.Sentry;
import com.lee.dutylower.db.greenEntity.Soldier;

import com.lee.dutylower.db.greenEntity.DutySoldierRecord;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "DUTY_SOLDIER_RECORD".
*/
public class DutySoldierRecordDao extends AbstractDao<DutySoldierRecord, Long> {

    public static final String TABLENAME = "DUTY_SOLDIER_RECORD";

    /**
     * Properties of entity DutySoldierRecord.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property SoldierID = new Property(1, Long.class, "soldierID", false, "SOLDIER_ID");
        public final static Property SentryID = new Property(2, Long.class, "sentryID", false, "SENTRY_ID");
        public final static Property Counts = new Property(3, Integer.class, "counts", false, "COUNTS");
        public final static Property Daofweek = new Property(4, Integer.class, "daofweek", false, "DAOFWEEK");
    }

    private DaoSession daoSession;


    public DutySoldierRecordDao(DaoConfig config) {
        super(config);
    }
    
    public DutySoldierRecordDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"DUTY_SOLDIER_RECORD\" (" + //
                "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
                "\"SOLDIER_ID\" INTEGER," + // 1: soldierID
                "\"SENTRY_ID\" INTEGER," + // 2: sentryID
                "\"COUNTS\" INTEGER," + // 3: counts
                "\"DAOFWEEK\" INTEGER);"); // 4: daofweek
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"DUTY_SOLDIER_RECORD\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, DutySoldierRecord entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        Long soldierID = entity.getSoldierID();
        if (soldierID != null) {
            stmt.bindLong(2, soldierID);
        }
 
        Long sentryID = entity.getSentryID();
        if (sentryID != null) {
            stmt.bindLong(3, sentryID);
        }
 
        Integer counts = entity.getCounts();
        if (counts != null) {
            stmt.bindLong(4, counts);
        }
 
        Integer daofweek = entity.getDaofweek();
        if (daofweek != null) {
            stmt.bindLong(5, daofweek);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, DutySoldierRecord entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        Long soldierID = entity.getSoldierID();
        if (soldierID != null) {
            stmt.bindLong(2, soldierID);
        }
 
        Long sentryID = entity.getSentryID();
        if (sentryID != null) {
            stmt.bindLong(3, sentryID);
        }
 
        Integer counts = entity.getCounts();
        if (counts != null) {
            stmt.bindLong(4, counts);
        }
 
        Integer daofweek = entity.getDaofweek();
        if (daofweek != null) {
            stmt.bindLong(5, daofweek);
        }
    }

    @Override
    protected final void attachEntity(DutySoldierRecord entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public DutySoldierRecord readEntity(Cursor cursor, int offset) {
        DutySoldierRecord entity = new DutySoldierRecord( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1), // soldierID
            cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2), // sentryID
            cursor.isNull(offset + 3) ? null : cursor.getInt(offset + 3), // counts
            cursor.isNull(offset + 4) ? null : cursor.getInt(offset + 4) // daofweek
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, DutySoldierRecord entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setSoldierID(cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1));
        entity.setSentryID(cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2));
        entity.setCounts(cursor.isNull(offset + 3) ? null : cursor.getInt(offset + 3));
        entity.setDaofweek(cursor.isNull(offset + 4) ? null : cursor.getInt(offset + 4));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(DutySoldierRecord entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(DutySoldierRecord entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(DutySoldierRecord entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getSoldierDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T1", daoSession.getSentryDao().getAllColumns());
            builder.append(" FROM DUTY_SOLDIER_RECORD T");
            builder.append(" LEFT JOIN SOLDIER T0 ON T.\"SOLDIER_ID\"=T0.\"_id\"");
            builder.append(" LEFT JOIN SENTRY T1 ON T.\"SENTRY_ID\"=T1.\"_id\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected DutySoldierRecord loadCurrentDeep(Cursor cursor, boolean lock) {
        DutySoldierRecord entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        Soldier soldier = loadCurrentOther(daoSession.getSoldierDao(), cursor, offset);
        entity.setSoldier(soldier);
        offset += daoSession.getSoldierDao().getAllColumns().length;

        Sentry sentry = loadCurrentOther(daoSession.getSentryDao(), cursor, offset);
        entity.setSentry(sentry);

        return entity;    
    }

    public DutySoldierRecord loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<DutySoldierRecord> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<DutySoldierRecord> list = new ArrayList<DutySoldierRecord>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<DutySoldierRecord> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<DutySoldierRecord> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
